<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:40
 */

namespace addons\admin\controller;


use app\common\AliPayAccount;
use app\common\Code;
use app\common\model\Trade;
use app\common\model\UserApplyCash;
use app\common\model\UserFund;
use app\common\model\Users;
use think\Db;

class Finance extends Common
{
    /**
     * 商家交易明细
     */
    public function trade_list()
    {
        $this->check_login();
        $param = input();
        $this->assign("param", $param);
        $trade_model = new Trade();
        $this->assign("trade", $trade_model);
        $trade_list = Trade::trade_list($param);
        $this->assign("trade_list", $trade_list);
        return $this->fetch();
    }

    /**
     * 申请提现列表
     */
    public function cash_list()
    {
        $param = input();
        $this->assign("param", $param);
        $list_ = UserApplyCash::cash_list($param);
        $this->assign("list_", $list_);
        return $this->fetch();
    }

    /**
     * 审核提现视图
     */
    public function examine_cash_view()
    {
        $param = input();
        $cash_ = UserApplyCash::get_cash_by_id($param['id']);
        $this->assign("cash_", $cash_);   #用户申请提现信息
        $user_ = Users::get_user_by_id($cash_->user_id);
        $this->assign("user_", $user_);   #用户信息
        $fund_ = UserFund::get_fund_by_user_id($cash_->user_id);
        $this->assign("fund_", $fund_);   #用户资金信息
        $alipay_account = json_decode($fund_->alipay_account, true);
        $this->assign("alipay_account", $alipay_account);   #用户支付宝信息
        return $this->fetch();
    }

    /**
     * 审核提现
     */
    public function examine_cash()
    {
        $param = input();
        // 启动事务
        Db::startTrans();
        try {
            \addons\admin\service\UserApplyCash::examine_cash($param['id'], $param);

            if ($param['status'] == 2)   #未通过审核
            {
                $out_biz_no = "alipayfailed" . date("YmdHis") . rand(10000, 99999);
                #提现金额判断
                $cash_ = UserApplyCash::get_cash_by_id($param['id']);     #提现信息
                /** @var Users $user_ */
                $user_ = Users::get_user_by_id($cash_->user_id);
                $remark = isset($param['memo']) ? $param['memo'] : "";
                Trade::create_alipay_tixian_success($user_, $out_biz_no, 0, sprintf("提现失败：%s", $remark));
            }
            if ($param['status'] == 1)   #通过审核
            {
                #提现金额判断
                $cash_ = UserApplyCash::get_cash_by_id($param['id']);     #提现信息
                /** @var Users $user_ */
                $user_ = Users::get_user_by_id($cash_->user_id);
                $fund_ = UserFund::get_fund_by_user_id($cash_->user_id);  #用户资金信息
                if ($cash_->value > $fund_->value) {
                    response_json(Code::YU_E_BUZU);
                }
                if (!$fund_->alipay_account) {
                    response_json(Code::VALID_ERROR, [], "此用户没有绑定支付宝");
                }
                $alipay_account = AliPayAccount::build_by_json($fund_->alipay_account);
                $fund = $user_->getUserFund();

                $zhuan_zhang_value = $cash_->value;
                #平台转账操作(转账方法中存了交易记录)
                $this->zhuan_zhang(
                    $alipay_account,
                    $zhuan_zhang_value,
                    function ($out_biz_no, $money, $remark) use ($user_, $fund, $zhuan_zhang_value) {
                        Trade::create_alipay_tixian_success($user_, $out_biz_no, $money, $remark);
                        $fund->setInc("value", 0 - $zhuan_zhang_value);
                    },
                    function ($resultCode, $sub_msg, $msg) {
                        response_json(Code::FAIL, [
                            "resultCode" => $resultCode,
                            "sub_msg" => $sub_msg,
                            "msg" => $msg,
                        ], $sub_msg);
                    }
                );
            }
            Db::commit();
            response_json(Code::NONE);
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            response_json(Code::FAIL);
        }

    }

}